package com.wenlong.agricultural.controller;

import com.wenlong.agricultural.pojo.OrderInfo;
import com.wenlong.agricultural.pojo.ShoppingCartInfo;
import com.wenlong.agricultural.pojo.UserInfo;
import com.wenlong.agricultural.service.OrderService;
import com.wenlong.agricultural.service.ShoppingCartService;
import com.wenlong.agricultural.utils.ResponseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 苏文龙
 * @version 1.0
 */
@Controller
@RequestMapping("/agricultural")
public class CartAndOrderController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/toResult")
    public String toResult() {
        return "result";
    }

    @RequestMapping("/confirmOrder")
    public String confirmOrder(@RequestParam("address") String address,
                               @RequestParam("payForWays") String payForWays,
                               @RequestParam("uid") String uid) {
        ValueOperations operations = redisTemplate.opsForValue();
        OrderInfo orderInfo = (OrderInfo) operations.getAndDelete(uid);
        orderInfo.setAddress(address);
        orderInfo.setPayForWays(payForWays);
        if (orderService.save(orderInfo)) {
            return "result";
        } else {
            throw new RuntimeException("订单持久化出问题");
        }
    }

    @RequestMapping("/goLookOrderForm")
    public ModelAndView goLookOrderForm(OrderInfo orderInfo, ModelAndView model) {
        model.setViewName("lookOrderForm");
        ValueOperations operations = redisTemplate.opsForValue();
        operations.set(orderInfo.getUid().toString(), orderInfo, 20, TimeUnit.MINUTES);
/*        OrderInfo orderInfo1 = (OrderInfo) operations.get(orderInfo.getUid().toString());
        System.out.println(orderInfo1);*/
        model.addObject("order", orderInfo);
        return model;
    }

    @RequestMapping("/deleteCartById")
    public String deleteCartById(@RequestParam("id") String id) {
        if (shoppingCartService.removeById(id)) {
            return "redirect:/agricultural/goLookShoppingCar";
        } else {
            throw new RuntimeException("无法通过id删除购物车");
        }
    }

    @RequestMapping("/goLookShoppingCar")
    public String goLookShoppingCar(HttpServletRequest request) {
        HttpSession session = request.getSession();
        UserInfo user = (UserInfo) session.getAttribute("user");
        List<ShoppingCartInfo> carts = shoppingCartService.getCartsByUserId(user.getId());
        request.setAttribute("carts", carts);
        return "lookShoppingCar";
    }


    @RequestMapping("/inCartInfo")
    @ResponseBody
    public ResponseInfo inCartInfo(ShoppingCartInfo shoppingCartInfo) {
        if (shoppingCartService.save(shoppingCartInfo)) {
            return ResponseInfo.ok()._massage("加入购物车成功");
        } else {
            return ResponseInfo.error()._massage("加入购物车失败");
        }
    }
}
